<?php

class Controller_Voice extends FLEA_Controller_Action
{
   function actionindex(){
   }
  function actiononlyword(){
        $table_words = & FLEA::getSingleton('Table_tofoonlywordvoice');
        $num = $table_words->findCount();
        for($i=0;$i<$num;$i=$i+100){
            echo $i."<br>";
            $words = $table_words->findAll(null,null,array("100",$i));
            foreach($words as $value){
                if(!safe_file_put_contents("_Shared/_Cache/wordvoice/" . $value[id] . ".wav",$value[VoiceData])){
                    echo $value[id]."<br>";
                }
            }
        }
        echo $num;
   }
   function actiononlywordupdate(){
        $start = $_GET['id'];
        $table_words = & FLEA::getSingleton('Table_tofoonlywordvoice');
        $num = $table_words->findCount();
        // $num = 20;
        for($i=$start;$i<$num;$i=$i+100){
            echo $i."<br>";
            $words = $table_words->findAll(null,null,array("100",$i),"id");
            foreach($words as $value){
                $row[id] = $value[id];
                $row[VoiceData] = safe_file_get_contents("_Shared/_Cache/wordvoice/" . $value[id] . ".mp3");
                if($table_words->save($row)){
                }
                else{
                    echo "<red>error:</red>".$row[id]."\t";
                }
            }
        }
        echo $num;
        echo "ok";
   }
   function actionpic(){
   }
   function actionpicupdate(){
        $start = $_GET['id']?$_GET['id']:0;
        $all = $start+20;

        $path = "_Shared/_Cache/picvoice/";
        $table_words = & FLEA::getSingleton('Table_tofowordvoice');
        $num = $table_words->findBySql("select count(*) as num from tofoword_text as a inner join tofoword_voice as b on a.Word=b.Word and a.No=b.No and a.Word in (select Word from gread_1_words)");
        $num = $num[0][num];
        $all = $all?$all:$num;
        // $num = 20;
        for($i=$start;$i<$all;$i=$i+10){
            print "from {$i} start ...<br>";
            $words = $table_words->findBySql("select b.id from tofoword_text as a inner join tofoword_voice as b on a.Word=b.Word and a.No=b.No and a.Word in (select Word from gread_1_words) limit {$i},10");
            foreach($words as $value){
                if(file_exists($path.$value[id].".mp3")){
                    $row[id] = $value[id];
                    $row[VoiceData] = safe_file_get_contents($path . $value[id] . ".mp3");
                    $row[Type] = "mp3";
                    if($table_words->update($row)){
                        print "sucess-update:{$value[id]}<br>";
                    }
                    else{
                        print "<red>error-update:</red>{$value[id]}<br>";
                    }
                }
                else{
                    print "error-noexists:{$value[id]}<br>";
                }
            }
        }
        print "{$all}/{$num} complete<br>";
        if($all < $num){
            redirect(url('voice','picupdate',array('id'=>$all)),2);
        }
   }
   function actionsentence(){
   }
   function actionsentenceupdate(){
        $start = $_GET['id']?$_GET['id']:0;
        $all = $start+20;

        $path = "_Shared/_Cache/sentencevoice/";
        $table_words = & FLEA::getSingleton('Table_wordsentence');
        $num = $table_words->findBySql("select count(*) as num from word_examplesentence as a where a.Word in (SELECT Word FROM `gread_1_words`)");
        $num = $num[0][num];
        $all = $all?$all:$num;
        // $num = 20;
        for($i=$start;$i<$all;$i=$i+10){
            print "from {$i} start ...<br>";
            $words = $table_words->findBySql("select id from word_examplesentence as a where a.Word in (SELECT Word FROM `gread_1_words`) limit {$i},10");
            foreach($words as $value){
                if(file_exists($path.$value[id].".mp3")){
                    $row[id] = $value[id];
                    $row[voicedata] = safe_file_get_contents($path . $value[id] . ".mp3");
                    $row[voicetype] = "mp3";
                    if($table_words->update($row)){
                        print "sucess-update:{$value[id]}<br>";
                    }
                    else{
                        print "<red>error-update:</red>{$value[id]}<br>";
                    }
                }
                else{
                    print "error-noexists:{$value[id]}<br>";
                }
            }
        }
        print "{$all}/{$num} complete<br>";
        if($all < $num){
            redirect(url('voice','sentenceupdate',array('id'=>$all)),3);
        }
   }
}
